cd "C:\Users\Public\Documents\ImmPanelRevis19\"
/**************************************/
/* Table de passeage Ze -> dep */
use dep comt using "ImmPanelRevis19\Donnees\indivm.dta", clear
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "ImmPanelRevis19\Donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop zen
contract ze dep

gsort ze - _freq
quietly by ze: keep if _n==1
drop _freq
save "ImmPanelRevis19\Donnees\CWT_dep_ze.dta", replace

use "ImmPanelRevis19\Donnees\indivm.dta", clear
/**************************************/
/* Définition des zones géographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "ImmPanelRevis19\Donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop zen

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
/* blue collar dummy */
gen ouv=(cs2h=="62" | cs2h=="63" | cs2h=="64" | cs2h=="65" | cs2h=="67" | cs2h=="68" | cs2h=="69" )

/* share of workers in Construction, Tradable and non-Tradable sector */
/* construction */
gen sconst=(nap40=="24")
/* tradable */
gen strad=((nap40=="01" | nap40=="02" | ///
nap40=="03" | nap40=="04" | nap40=="05" | nap40=="06" | ///
nap40=="07" | nap40=="08" | nap40=="09" | nap40=="10" | ///
nap40=="11" | nap40=="12" | nap40=="13" | nap40=="14" | ///
nap40=="15" | nap40=="16" | nap40=="17" | nap40=="18" | ///
nap40=="19" | nap40=="20" | nap40=="21" | nap40=="22" | ///
nap40=="23" | nap40=="36" | nap40=="37" | nap40=="35" | nap40=="32"))
/* non-tradable */
gen sntrad=((nap40=="24" | nap40=="25" | ///
nap40=="26" | nap40=="27" | nap40=="28" | nap40=="29" | ///
nap40=="30" | nap40=="31" | nap40=="33" | nap40=="34"))

/* save old ze definition for region FE */
gen Xze=ze
replace ze=dep
save temp.dta, replace

/* share workers in tradable, non-tradable and construction sector */
use temp.dta, clear
collapse (mean) sconst strad sntrad , by(ze an) fast
save dep_sect.dta, replace

/* number of employees per commuting zone */
use temp.dta, clear
gen emp=1
gen lsnd=ln(sn/dp)
collapse (count)  n_emp=emp (mean) mlsnd=lsnd , by(ze an) fast
/* adjust sampling rates */
replace n_emp=n_emp*24 if an!=2007
replace n_emp=n_emp*12 if an==2007
save dep_nemp.dta, replace

/* Shift-share importé */
use "ImmPanelRevis19\Donnees\pit\shiftshare2017.dta", clear
/* immigrants employed per commuting zone */
joinby ze an using "ImmPanelRevis19\Donnees\pit\RED_zepopemp2017.dta", unm(m)

drop _merge

joinby ze using "ImmPanelRevis19\Donnees\CWT_dep_ze.dta"
collapse (sum) zepop immig pit68 pit75 pit82 pit90 pit99 pit168 ///
pit268 pit175 pit275 pit182 pit282 pit190 pit290 pit199 ///
pit299 opit68 opit75 opit82 opit90 opit99 nimptm1 nimp68 natemp imemp, by(dep an)
rename dep ze

/* adjust census year to DADS years */
replace an=1991 if an==1990
replace an=1976 if an==1975
joinby ze an using dep_nemp.dta, unm(m)
drop if an==2012
drop _merge
joinby ze an using dep_sect.dta, unm(m)
drop _merge

rm dep_nemp.dta
rm dep_sect.dta

/* define panel */
egen ize=group(ze)
egen time=group(an)
xtset ize time

replace n_emp=F.n_emp if an==1968
/* variable d'intérêt: changement du nombre d'employés immigrés */
gen dimm=D.imemp/(L.n_emp)
gen zeimmig=immig/zepop

/* instruments */
gen dimm68=pit68/L.n_emp
gen dimm75=pit75/L.n_emp
gen dimm82=pit82/L.n_emp
gen dimm90=pit90/L.n_emp
gen dimm99=pit99/L.n_emp

gen ldimm=L.dimm
gen ldimm68=L.dimm68
gen ldimm75=L.dimm75
gen ldimm82=L.dimm82
gen ldimm90=L.dimm90
gen ldimm99=L.dimm99

/* replace ldimm=D.imemp/(F.n_emp) if missing(ldimm) */

reg dimm82 ldimm82
predict pdimm82, residuals

/* instrument baseline t-2 */
gen dimtm2=dimm68 if an==1975
replace dimtm2=dimm68 if an==1982
replace dimtm2=dimm75 if an==1991
replace dimtm2=dimm82 if an==1999
replace dimtm2=dimm90 if an==2007

gen ldimtm2=ldimm68 if an==1975
replace ldimtm2=ldimm68 if an==1982
replace ldimtm2=ldimm75 if an==1991
replace ldimtm2=ldimm82 if an==1999
replace ldimtm2=L.dimm90 if an==2007

gen lsconst=L.sconst
gen lstrad=L.strad
gen lsntrad=L.sntrad
gen lzepop=ln(L.zepop)

keep an ze dimm dimm68 dimm75 ///
ldimm ldimm68 ldimm75 ldimtm2 dimtm2 zepop ///
lsconst lstrad lsntrad zeimmig zepop lzepop mlsnd

save "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", replace
rm temp.dta

/* adjust for 92 and 93 */
use "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", clear
keep if ze=="75"
replace ze="92"
save temp92.dta, replace
use "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", clear
keep if ze=="75"
replace ze="93"
save temp93.dta, replace
use "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", clear
append using temp92.dta
append using temp93.dta
rm temp92.dta
rm temp93.dta
sort ze an
save "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", replace
